package com.demo.springboot.resttemplate.controller;

import com.demo.springboot.resttemplate.entity.User;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * RestTemplate server
 * @author WangGang
 * @date 2021/10/27
 */
@RestController
@RequestMapping("server")
public class ServerController {
    /**
     * get user
     * @author WangGang
     * @date 2021/10/27
     */
    @GetMapping("getUser")
    public String getUser(HttpServletRequest request){
        String token=request.getHeader("token");
        return "youku1327";
    }

    /**
     * get user by name
     * @param name 姓名
     * @author WangGang
     * @date 2021/10/27
     */
    @GetMapping("getUserByName")
    public String getUserByName(HttpServletRequest request,String name){
        String token=request.getHeader("token");
        return "welcome "+name;
    }

    /**
     * add user
     * @author WangGang
     * @date 2021/10/27
     */
    @PostMapping("addUser")
    public ResponseEntity<User> addUser(@RequestBody User user, HttpServletRequest request){
        String token=request.getHeader("token");

        return ResponseEntity.ok(user);
    }

    @PostMapping("addUserByForm")
    public ResponseEntity<User> addUserByForm(User user, HttpServletRequest request){
        String token=request.getHeader("token");

        return ResponseEntity.ok(user);
    }

    /**
     * update user
     * @author WangGang
     * @date 2021/10/27
     */
    @PutMapping("updateUser")
    public ResponseEntity<User> updateUser(@RequestBody User user, HttpServletRequest request){
        String token=request.getHeader("token");
        return ResponseEntity.ok(user);
    }

}
